package database

import (
	"fmt"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
)

type Classmate struct {
	Id        int
	Name      string
	Sn        string
	Sex       string
	Classname string
}

func DEDEmo() {
	dsn := "root:123456@tcp(172.14.1.65:3306)/classmates?charset=utf8mb4&parseTime=True&loc=Local"
	db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
	if err != nil {
		fmt.Println("连接数据库失败，原因是:", err.Error())
		return
	}
	fmt.Println("成功", db)

	//var zhangsan Classmate
	//zhangsan.Name = "张三"
	//zhangsan.Sex = "男"
	//zhangsan.Sn = "123456"
	//tx := db.Save(&zhangsan)
	//fmt.Println(tx)
	//-----查询
}

var db *gorm.DB

func Login() bool {
	var name string
	fmt.Println("姓名")
	fmt.Scanln(&name)
	var pswd string
	fmt.Println("密码")
	fmt.Scanln(&pswd)
	var user Classmate
	db.Raw("SELECT *FROM classmates. classmates where name= ?", name).Scan(&user)
	if user.Pswd == pswd {
		fmt.Println("成功", user.Name)
		return true
	} else {
		fmt.Println("失败")
		return false
	}
}
